import { mat2d, vec2 } from '@/libs/glmatrix/gl-matrix-min.js'
import { ref, computed } from 'vue'
export default class Scale {
  constructor(box) {
    this.nowMat = ref(mat2d.create())
    this.style = computed(() => {
      return {
        transform: `matrix(${this.nowMat.value.join(',')})`
      }
    })
    this.history = []
    this.set(box)
  }
  save() {
    this.history.push(mat2d.clone(this.nowMat.value))
  }
  withdraw() {
    let value = this.history.pop()
    if (value) this.nowMat.value = value
  }
  set(box) {
    this.box = box
    this.nowMat.value = box.nowMat ?? mat2d.create()
  }
  large() {
    this.scale([1.2, 1.2])
  }
  up() {
    this.move([0, -10])
  }
  down() {
    this.move([0, 10])
  }
  //旋转永远是中心旋转
  rotate(rad = Math.PI / 6) {
    let endMat = mat2d.create()
    mat2d.mul(endMat, endMat, this.nowMat.value)
    mat2d.rotate(endMat, endMat, rad)
    this.nowMat.value = endMat
  }
  move(v) {
    let endMat = mat2d.create()
    mat2d.translate(endMat, endMat, v)
    mat2d.mul(endMat, endMat, this.nowMat.value)
    this.nowMat.value = endMat
  }
  scale(scaleV) {
    let v = vec2.create()
    vec2.sub(v, this.box.scaleCenter, this.box.center)
    let m = mat2d.create()
    mat2d.translate(m, m, v)
    let m_1 = mat2d.create()
    mat2d.invert(m_1, m)
    let scaleMat = mat2d.create()
    mat2d.scale(scaleMat, scaleMat, scaleV)
    let endMat = mat2d.create()
    mat2d.mul(endMat, endMat, m)
    mat2d.mul(endMat, endMat, scaleMat)
    mat2d.mul(endMat, endMat, m_1)
    mat2d.mul(endMat, endMat, this.nowMat.value)
    this.nowMat.value = endMat
  }
  small() {
    this.scale([1 / 1.2, 1 / 1.2])
  }
}

export class Abc extends Scale {
  constructor() {
    super()
  }
}
